#include "Sequence.h"
#include "T_Scene.h"

Sequence *Sequence::create(T_Action *a,...){
	Sequence *sequ=new Sequence();
	T_Action **p=&a;
	while(*p!=NULL){
		sequ->m_Actions.push_back(*p);
		p++;
	}
	return sequ;
}

Sequence::~Sequence(){
	for(std::vector<T_Action *>::iterator i=m_Actions.begin();i!=m_Actions.end();i++){
		(*i)->release();
	}
	m_Actions.clear();
	m_Actions.swap(std::vector<T_Action *>());
}

bool Sequence::init(T_Node *node){
	if(NULL==node)return false;
	setTarget(node);
	return true;
}

bool Sequence::isDone()const{
	return m_Actions.begin()==m_Actions.end();
}

void Sequence::startWithTarget(T_Node *node){
	if(!init(node))return;
	T_Scene *scene=node->getScene();
	if(NULL==scene){
		return;
	}
	ActionPool &pool=scene->getActionPool();
	pool.removeAll(node,this->getActionType());
	pool.add(this);
	if(m_Actions.begin()!=m_Actions.end()){
		m_Actions.front()->init(node);
	}
}

void Sequence::update(int dt){
	if(m_Actions.begin()!=m_Actions.end()){
		T_Action *action=m_Actions.front();
		action->update(dt);
		if(action->isDone()){
			m_Actions.erase(m_Actions.begin());
			action->release();
			if(m_Actions.begin()!=m_Actions.end()){
				m_Actions.front()->init(m_Target);
			}
		}
	}
}